package com.yupi.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.yupi.project.common.BaseResponse;
import com.yupi.project.common.DeleteRequest;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.dto.type.TypeAddrequest;
import com.yupi.project.model.dto.type.TypeUpdateRequest;
import com.yupi.project.model.dto.user.*;
import com.yupi.project.model.entity.Product;
import com.yupi.project.model.entity.Type;
import com.yupi.project.model.entity.User;
import com.yupi.project.model.vo.TypeVo;
import com.yupi.project.model.vo.UserVO;
import com.yupi.project.service.ProductService;
import com.yupi.project.service.TypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;



@RestController
@RequestMapping("type")
@CrossOrigin
public class TypeController {
    @Resource
    private TypeService typeService;

    @Resource
    private ProductService productService;


    /**
     * 增加类别
     *
     * @return
     */
    @PostMapping("")
    public BaseResponse<Long> addType(@RequestBody TypeAddrequest typeAddrequest) {

        if (typeAddrequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (!StringUtils.isNotBlank(typeAddrequest.getName())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "类别名称不能为空");
        }

        Type type = new Type();
        type.setName(typeAddrequest.getName());

        boolean save = typeService.save(type);
        return ResultUtils.success(type.getId());
    }

    /**
     * 修改类别
     *
     * @return
     */
    @PutMapping("")
    public BaseResponse<Long> addType(@RequestBody TypeUpdateRequest typeUpdateRequest) {

        if (typeUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (typeUpdateRequest.getId() == null || typeUpdateRequest.getId() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该类别不存在");
        }

        Type type = new Type();
        BeanUtils.copyProperties(typeUpdateRequest, type);

        boolean save = typeService.updateById(type);
        if (save) {
            return ResultUtils.success(type.getId());
        } else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该类别不存在");
        }

    }



    /**
     * 删除类别
     *
     * @return
     */
    @DeleteMapping("")
    public BaseResponse<Boolean> deleteType(Long id) {

        if (id == null || id == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该类别不存在");
        }

//        先查询该类别下是否有商品
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getTypeid, id);
        long count = productService.count(queryWrapper);
        if (count != 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请先删除该类别下其他商品");
        }

        boolean b = typeService.removeById(id);
        if (!b) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该类别不存在");
        }

        return ResultUtils.success(b);


    }



    /**
     * 删除类别
     *
     * @return
     */
    @GetMapping("")
    public BaseResponse<List<TypeVo>> getAllType() {

        List<Type> list = typeService.list();
        List<TypeVo> collect = list.stream().map(type -> {
            TypeVo typeVo = new TypeVo();
            BeanUtils.copyProperties(type, typeVo);

            return typeVo;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }


}
